//
//  LBContactViewController.m
//  app
//
//  Created by LINAICAI on 16/9/21.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBContactViewController.h"
#import "LBContactManager.h"
#import "LBCustomNavImageView.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import <Masonry.h>
#import <LBCoreKit.h>
#import <LBAlert.h>
#import <LBLoadingView.h>
@interface LBContactViewController ()
@property (nonatomic, strong)LBCustomNavImageView *customNavView;
@property(nonatomic , strong)UIView *headView;
@property (nonatomic , strong)UILabel *nameLabel;
@property (nonatomic , strong)UITextField *phoneTextField;
@property (nonatomic , strong)UITextField *jobTextField;
@property (nonatomic , strong)UITextField *companyTextField;
@property (nonatomic , strong)UIImageView *authImageView;
@property (nonatomic,strong)UIButton *rightButton;
@property (nonatomic , strong)LBContactModel *model;
@property (nonatomic , assign)BOOL canEditing;
@end

@implementation LBContactViewController
#pragma mark- Getting|Setting

- (LBCustomNavImageView *)customNavView {
    if (!_customNavView) {
        _customNavView = [[LBCustomNavImageView alloc] initWithTitle:@"联系人" bgImage:LBBeeImage(@"BeeApplyPartner.png") rightItem:self.canEditing ? self.rightButton : nil];
    }
    return _customNavView;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton setTitle:@"编辑" forState:UIControlStateNormal];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _rightButton.frame = CGRectMake(0, 0, 80, 64);
        [_rightButton addTarget:self action:@selector(editButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

- (UILabel *)leftViewWithLabel:(NSString *)text{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];
    label.text = text;
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}
- (UIImageView *)authImageView{
    if (!_authImageView) {
        _authImageView = [[UIImageView alloc]init];
        _authImageView.image = LBUserImage(@"conAuthVip");
        _authImageView.hidden = YES;
    }
    return _authImageView;
}
- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]init];
    }
    return _headView;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = [UIFont boldSystemFontOfSize:20];
    }
    return _nameLabel;
}
- (UITextField *)phoneTextField{
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc]init];
        _phoneTextField.leftView = [self leftViewWithLabel:@"手机"];
        [_phoneTextField setLeftViewMode:UITextFieldViewModeAlways];
        [_phoneTextField setKeyboardType:UIKeyboardTypeNumberPad];
    }
    return _phoneTextField;
}
- (UITextField *)jobTextField{
    if (!_jobTextField) {
        _jobTextField = [[UITextField alloc]init];
        _jobTextField.leftView = [self leftViewWithLabel:@"职位"];
        [_jobTextField setLeftViewMode:UITextFieldViewModeAlways];
    }
    return _jobTextField;
}
- (UITextField *)companyTextField{
    if (!_companyTextField) {
        _companyTextField = [[UITextField alloc]init];
        _companyTextField.leftView = [self leftViewWithLabel:@"公司"];
        [_companyTextField setLeftViewMode:UITextFieldViewModeAlways];
    }
    return _companyTextField;
}
- (LBContactModel *)model{
    if (!_model) {
        _model = [LBContactModel new];
    }
    return _model;
}
#pragma mark- 生命周期
- (instancetype)initWithModel:(LBContactModel *)model
{
    self = [super init];
    if (self) {
        self.model = model;
        self.canEditing = NO;
    }
    return self;
}
- (instancetype)initWithModel:(LBContactModel *)model canEditing:(BOOL)canEditing{
    self = [super init];
    if (self) {
        self.model = model;
        self.canEditing = canEditing;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    
    [self.view addSubview:self.customNavView];
    [self.view addSubview:self.headView];
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.phoneTextField];
    [self.view addSubview:self.jobTextField];
    [self.view addSubview:self.companyTextField];
    [self.view addSubview:self.authImageView];
    
    [self.customNavView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.view);
    }];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(150);
        make.top.mas_equalTo(self.customNavView.lbNavBar.mas_bottom);
        make.bottom.mas_equalTo(self.customNavView.mas_bottom);
    }];

    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.headView.mas_bottom);
        
    }];
    [self.jobTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.phoneTextField.mas_bottom);
        
    }];
    [self.companyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.jobTextField.mas_bottom);
        
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.right.equalTo(self.view);
        make.height.mas_equalTo(30);
        make.bottom.mas_equalTo(self.headView.mas_bottom).mas_offset(-40);
    }];
    [self.authImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(66, 21));
        make.bottom.mas_equalTo(self.headView.mas_bottom).mas_offset(-40);
    }];
    
    // Do any additional setup after loading the view.
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self setUIWithModel:self.model];
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark- Private
- (void)editButton:(UIButton *)sender{
    [self.phoneTextField setEnabled:YES];
    [self.jobTextField setEnabled:YES];
    [self.companyTextField setEnabled:YES];
    
    self.phoneTextField.placeholder = @"请输入手机号";
    self.jobTextField.placeholder = @"请输入职位(选填)";
    self.companyTextField.placeholder = @"请输入所在公司(选填)";
    
    if ([sender.titleLabel.text isEqualToString:@"编辑"]) {
        [sender setTitle:@"完成" forState:UIControlStateNormal];
        [self.phoneTextField becomeFirstResponder];
    }
    else if ([sender.titleLabel.text isEqualToString:@"完成"]) {
        [self doneButton:sender];
    }
}
- (void)doneButton:(id)sender{
    if (!_phoneTextField.text.length) {
        [LBAlert say:@"请填写联系人手机号码!"];
        return;
    }
    else if (![_phoneTextField.text validateMobileNumber]){
        [LBAlert say:@"请填写正确的手机号码!"];
        return;
    }
    [LBLoadingView showInView:self.view];
    
    self.model.company = self.companyTextField.text;
    self.model.lable = self.jobTextField.text;
    self.model.mobilePhone = self.phoneTextField.text;
    
    WS(weakSelf);
    [LBContactManager updateContactWithId:weakSelf.model.id
                                               company:weakSelf.model.company
                                                 lable:weakSelf.model.lable
                                           mobilePhone:weakSelf.model.mobilePhone
                                                  name:weakSelf.model.name
                                               success:^(NSString *message)
    {
        [LBLoadingView dismiss];
        [LBAlert say:^(NSInteger answer){
            [weakSelf.phoneTextField setEnabled:NO];
            [weakSelf.jobTextField setEnabled:NO];
            [weakSelf.companyTextField setEnabled:NO];
            
            weakSelf.phoneTextField.placeholder =
            weakSelf.jobTextField.placeholder =
            weakSelf.companyTextField.placeholder = @"";
            [weakSelf.rightButton setTitle:@"编辑" forState:UIControlStateNormal];
        } message:@"修改联系人成功"];
        
    } fail:^(NSString *message) {
        [LBLoadingView dismiss];
        [LBAlert say:message];
    }];
    

}
- (void)setUIWithModel:(LBContactModel *)model{
    self.nameLabel.text = LBStringNotNil(model.name);
    self.phoneTextField.text = LBStringNotNil(model.mobilePhone);
    self.jobTextField.text = LBStringNotNil(model.lable);
    self.companyTextField.text = LBStringNotNil(model.company);
    
    [self.phoneTextField setEnabled:NO];
    [self.jobTextField setEnabled:NO];
    [self.companyTextField setEnabled:NO];
    
    if ([LBStringNotNil(model.realNameFlag) isEqualToString:@"1"]) {
        self.authImageView.hidden = NO;
    }
    else{
        self.authImageView.hidden = YES;
    }
    
}

@end
